/**
 * TreeBySHF Library v1.0.0
 * http://cnblogs.com/iamshf
 *
 * author:盛浩峰
 * Date: 2013-04-19
 */
var IMAGEPATH = "../js/TreeBySHF/Images";
var STYLEPATH = "../js/TreeBySHF/TreeBySHF.css";
$("<link>")
    .attr({ rel: "stylesheet",
        type: "text/css",
        href: STYLEPATH
    }).appendTo("head");
    $.extend({ TreeBySHF: {
        BindTree: function (params) {
            var defaultParams = { tvData: {}, TreeContainerID: "", OptParamValue: 0, PID: "0", isExpansion: false, funName: new Object() };
            $.extend(defaultParams, params);
            var dataLength = defaultParams.tvData.length;
            var StartNo = 0;
            BindRoot();
            SetCheckBox();
            $.TreeBySHF.SetOpt(defaultParams.OptParamValue);
            SetIsExpansionNode(defaultParams.isExpansion);
            function BindRoot() {
                var parentTag = defaultParams.PID + "-";
                var html = GetChildrenNode(defaultParams.PID, parentTag);
                if (html.length > 0) {
                    $("#" + defaultParams.TreeContainerID).html("<ul id=\"TreeBySHF\" class=\"TreeBySHF\" parentTag=\"" + parentTag + "\">" + html + "</ul>");
                    BindChildren(parentTag, true);
                }
            };
            function BindNode(objContainer, PID, parentTag, imgName) {
                var parentTag = parentTag + PID + "-";
                var html = GetChildrenNode(PID, parentTag);
                if (html.length > 0) {
                    objContainer.prepend("<img class=\"Ex_Cos_Ico\" src=\"" + IMAGEPATH + "/Opened" + imgName + ".gif\">").append("<ul parentTag=\"" + parentTag + "\">" + html + "</ul>");
                    SetClickEvent(objContainer, imgName);
                    BindChildren(parentTag, false);
                }
                else {
                    if (imgName == "Last") {
                        imgName = "OpenLastNoChildrenLast";
                    }
                    else if (imgName == "First") {
                        imgName = "NoChildrenFirst";
                    }
                    else {
                        imgName = "LineMiddle";
                    }
                    objContainer.prepend("<img class=\"Node_Ico\" src=\"" + IMAGEPATH + "/" + imgName + ".gif\">");
                    objContainer.children("a").click(function () { ChooseNode($(this)); })
                }
            };
            //遍历给出的树，绑定子节点
            function BindChildren(parentTag, isRoot) {
                var obj = $("ul[parentTag='" + parentTag + "']").children("li");
                var objCount = obj.length;
                var isFirst = true;
                var isLast = false;
                for (var i = 0; i < objCount; i++) {
                    var imgName = GetImgName(isRoot, i == objCount - 1, i == 0);
                    BindNode($(obj[i]), $(obj[i]).children("a").attr("NODEID"), parentTag, imgName);
                }
            };
            //定义获取节点数据，并生成HTML
            function GetChildrenNode(PID, parentTag) {
                var html = "";
                for (var i = 0/*StartNo*/; i < dataLength; i++) {
                    /*if (defaultParams.tvData[i].PID > PID) {
                    	if(i==StartNo+1){
                            StartNo = i;
                        }
                        break;
                    }*/
                    if (defaultParams.tvData[i].PID == PID) {
                        html += "<li>";
                        html += "<input type=\"checkbox\" name=\"SelectedTreeCheckbox\" parentTag=\"" + parentTag + "\" value=\"" + defaultParams.tvData[i].NODEID + "\" />";
                        html += "<input type=\"radio\" name=\"SelectedTreeRadio\" value=\"" + defaultParams.tvData[i].NODEID + "\" />";
                        html += "<a href=\""+defaultParams.tvData[i].URL+"&nid="+defaultParams.tvData[i].NODEID+"\" target=\""+defaultParams.tvData[i].TARGET+"\" nodeID=\"" + defaultParams.tvData[i].NODEID + "\">" + defaultParams.tvData[i].NODENAME + "</a>";
                        html += "</li>";
                    }
                }
                return html;
            };
            //根据树不同的位置设置不同的图片
            function GetImgName(isRoot, isLast, isFirst) {
                if (isRoot && isFirst) {
                    return "First";
                }
                else if (isLast) {
                    return "Last";
                }
                else {
                    return "Middle";

                }
            };
            //设置点击展开关闭事件
            function SetClickEvent(obj, imgName) {
                obj.children("img").unbind("click").click(function () {
                    CloseNode(obj, imgName);
                });
                obj.children("a").unbind("click").click(function () {
                    CloseNode(obj, imgName);
                    ChooseNode($(this));
                });
            };
            function ExpansionNode(obj, imgName) {
                obj.children("img").attr("src", "" + IMAGEPATH + "/Opened" + imgName + ".gif").attr("isOpened", "1").unbind("click").click(function () {
                    CloseNode(obj, imgName);
                });
                obj.children("a").unbind("click").click(function () {
                    CloseNode(obj, imgName);
                    ChooseNode($(this));
                });
                obj.children("ul").show();
            };
            function CloseNode(obj, imgName) {
                obj.children("img").attr("src", "" + IMAGEPATH + "/Closed" + imgName + ".gif").attr("isOpened", "0").unbind("click").click(function () {
                    ExpansionNode(obj, imgName);
                });
                obj.children("a").unbind("click").click(function () {
                    ExpansionNode(obj, imgName);
                    ChooseNode($(this));
                });
                obj.children("ul").hide();
            };
            //定义选中事件
            function ChooseNode(obj) {
                //                $("#" + defaultParams.TreeContainerID + " .Selected").removeClass("Selected");
                //                $(obj).addClass("Selected");
                if ($.isFunction(defaultParams.funName)) {
                    defaultParams.funName(obj);
                }
            };
            //关联选中事件
            function SetCheckBox() {
                //                if (isShowOpt) {
                //                    if (OptType == "checkbox") {
                $("#TreeBySHF input[parentTag]").each(function () {
                    $(this).change(function () {
                        var parentTag = $(this).attr("parentTag");
                        var parentTagList = parentTag.split('-');
                        var parentTagLength = parentTagList.length - 1;
                        if ($(this).is(":checked")) {
                            for (var i = 0; i < parentTagLength; i++) {
                                $("#TreeBySHF input[parentTag][value='" + parentTagList[i] + "']").attr("checked", "checked");
                            }
                            $("#TreeBySHF input[parentTag^='" + parentTag + $(this).val() + "-']").attr("checked", "checked");
                        }
                        else {
                            for (var i = parentTagLength - 1; i > -1; i--) {
                                $("#TreeBySHF input[parentTag^='" + parentTag + $(this).val() + "-']").removeAttr("checked");
                                var tempObj = $("#TreeBySHF input[parentTag][value='" + parentTagList[i] + "']");
                                if ($("#TreeBySHF input[parentTag^='" + tempObj.attr("parentTag") + tempObj.val() + "-']:checked").length == 0) {
                                    tempObj.removeAttr("checked");
                                }
                            }
                        }
                    })
                })
                //                    }
                //                }
                //                else {
                $("#TreeBySHF input").hide();
                //                }
            };
            function SetIsExpansionNode(isExpansion) {
                if (!isExpansion) { $(".Ex_Cos_Ico").click(); }
            }
        },
        //如果为0，则不显示，如果为1则显示checkbox，如果为2，则显示radio
        SetOpt: function (value) {
            switch (value) {
                case 0:
                    $("#TreeBySHF input").hide();
                    break;
                case 1:
                    $("#TreeBySHF input[type='radio']").hide();
                    $("#TreeBySHF input[type='checkbox']").show();
                    break;
                default:
                    $("#TreeBySHF input[type='checkbox']").hide();
                    $("#TreeBySHF input[type='radio']").show();
            }
        }
    }
    });